const db = require("../../config/db");

// 获取企业股权关系
const getEnterpriseRelations = async (req, res) => {
  const { enterpriseId } = req.params;
  try {
    const [relations] = await db.execute("SELECT * FROM enterprise_relations WHERE enterprise_id = ?", [enterpriseId]);
    if (relations.length === 0) {
      return res.status(404).json({ message: "企业股权关系未找到" });
    }
    res.status(200).json(relations);
  } catch (error) {
    res.status(500).json({ message: "获取股权关系失败", error });
  }
};
//新增股权关系
const createEnterpriseRelation = async (req, res) => {
    const { enterpriseId, relations } = req.body;  // 解构请求体
  
    // 确保 relations 是一个正确的 JSON 格式
    if (!relations || !relations.upStreamJSON || !relations.downStreamJSON) {
      return res.status(400).json({ message: "股权关系格式错误" });
    }
  
    try {
      // 插入股权关系
      const [result] = await db.execute(
        "INSERT INTO enterprise_relations (enterprise_id, relations) VALUES (?, ?)",
        [enterpriseId, JSON.stringify(relations)]  // JSON.stringify 用于将 relations 对象转为 JSON 字符串
      );
  
      res.status(201).json({ message: "股权关系创建成功", id: result.insertId });
    } catch (error) {
      console.error("创建股权关系失败:", error);
      res.status(500).json({ message: "创建股权关系失败", error });
    }
  };
  
  

// 更新股权关系
const updateEnterpriseRelation = async (req, res) => {
    const { id, enterpriseId, relations } = req.body;  // 获取传入的 id 和 enterpriseId 以及 relations 数据
    console.log("Request Body:", req.body); // 打印请求体，查看传入的数据
  
    try {
      // 确保传入的 enterpriseId 和 relations 不为空
      if (!enterpriseId || !relations) {
        return res.status(400).json({ message: "enterpriseId 和 relations 必须提供" });
      }
  
      // 执行更新操作
      const [result] = await db.execute(
        "UPDATE enterprise_relations SET relations = ?, enterprise_id = ? WHERE id = ?",
        [JSON.stringify(relations), enterpriseId, id]  // 传入参数：relations、enterprise_id 和 id
      );
  
      if (result.affectedRows === 0) {
        return res.status(404).json({ message: "股权关系未找到" });
      }
  
      res.status(200).json({ message: "股权关系更新成功" });
    } catch (error) {
      console.error("更新股权关系失败:", error);  // 打印详细错误信息，便于调试
      res.status(500).json({ message: "更新股权关系失败", error: error.message });
    }
  };
  

//删除股权关系
const deleteEnterpriseRelation = async (req, res) => {
    const { enterpriseId } = req.params;
  
    if (!enterpriseId) {
      return res.status(400).json({ message: "企业ID 是必需的" });
    }
  
    try {
      // 删除股权关系
      const [result] = await db.execute(
        "DELETE FROM enterprise_relations WHERE enterprise_id = ?", [enterpriseId]
      );
  
      if (result.affectedRows === 0) {
        return res.status(404).json({ message: "股权关系未找到" });
      }
  
      res.status(200).json({ message: "股权关系删除成功" });
    } catch (error) {
      res.status(500).json({ message: "删除股权关系失败", error });
    }
  };

  //股权关系列表
  const getAllRelations = async (req, res) => {
    try{
      const [relations] = await db.execute("SELECT * FROM enterprise_relations");
      res.status(200).json({
        code:200,
        message:"获取成功",
        data:relations
      });
    }catch(error){
      console.error("获取所有股权关系失败：", error);
      res.status(500).json({ code:500,message:"获取失败",error});
    }
  }
module.exports = {
  getEnterpriseRelations,
  createEnterpriseRelation,
  updateEnterpriseRelation,
  deleteEnterpriseRelation,
  getAllRelations
};
